

import UIKit

protocol XSNewGuideCellDelegate: NSObjectProtocol
{
    func startBtnDidClick()
}

class XSNewGuideCell: UICollectionViewCell {
    
    weak var delegate: XSNewGuideCellDelegate?
    
    var index = 0
    {
        didSet // 相当于重写set方法 属性监视器 检测属性的改变
        {
            newGuideImage.image = UIImage(named: "new_feature_\(index + 1)")
            startBtn.hidden = true
        }
    }
    
    func startAnimate()
    {
        startBtn.hidden = false
        
        self.startBtn.transform = CGAffineTransformMakeScale(0, 0)
        
        UIView.animateWithDuration(1.0, delay: 0, usingSpringWithDamping: 0.5, initialSpringVelocity: 3, options: UIViewAnimationOptions(rawValue: 0), animations: { () -> Void in
            
            self.startBtn.transform = CGAffineTransformIdentity
            
            }, completion: { (_) -> Void in
                
        })
    }
    
    override init(frame: CGRect)
    {
        super.init(frame: frame)
        
        self.setup()
    }
    
    private func setup()
    {
        contentView.addSubview(newGuideImage)
        contentView.addSubview(startBtn)
        
        newGuideImage.translatesAutoresizingMaskIntoConstraints = false
        startBtn.translatesAutoresizingMaskIntoConstraints = false
        
        // newGuideImage 约束
        self.addConstraint(NSLayoutConstraint(item: newGuideImage, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: contentView, attribute: NSLayoutAttribute.Top, multiplier: 1, constant: 0))
        self.addConstraint(NSLayoutConstraint(item: newGuideImage, attribute: NSLayoutAttribute.Leading, relatedBy: NSLayoutRelation.Equal, toItem: contentView, attribute: NSLayoutAttribute.Leading, multiplier: 1, constant: 0))
        self.addConstraint(NSLayoutConstraint(item: newGuideImage, attribute: NSLayoutAttribute.Bottom, relatedBy: NSLayoutRelation.Equal, toItem: contentView, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 0))
        self.addConstraint(NSLayoutConstraint(item: newGuideImage, attribute: NSLayoutAttribute.Trailing, relatedBy: NSLayoutRelation.Equal, toItem: contentView, attribute: NSLayoutAttribute.Trailing, multiplier: 1, constant: 0))
        
        // startBtn 约束
        self.addConstraint(NSLayoutConstraint(item: startBtn, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: contentView, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        self.addConstraint(NSLayoutConstraint(item: startBtn, attribute: NSLayoutAttribute.Bottom, relatedBy: NSLayoutRelation.Equal, toItem: contentView, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: -200))
    }
    
    func startBtnClick()
    {
        delegate?.startBtnDidClick()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    // MARK: - 懒加载
    private lazy var newGuideImage: UIImageView = UIImageView(image: UIImage(named: "new_feature_1"))
    
    private lazy var startBtn: UIButton =
    {
        let startBtn = UIButton()
        
        startBtn.setBackgroundImage(UIImage(named: "new_feature_finish_button"), forState: UIControlState.Normal)
        startBtn.setBackgroundImage(UIImage(named: "new_feature_finish_button_highlighted"), forState: UIControlState.Highlighted)
        startBtn.setTitle("开始体验", forState: UIControlState.Normal)
        startBtn.titleLabel?.font = UIFont.systemFontOfSize(15)
        startBtn.setTitleColor(UIColor.whiteColor(), forState: UIControlState.Normal)
        startBtn.addTarget(self, action: "startBtnClick", forControlEvents: UIControlEvents.TouchUpInside)
        
        return startBtn
    }()
}
